%% Calculation of nonexpanding windows with arbitrary number of layers
clear
close all
clc

fig = figure;

set(fig,'Color','w');



q=[2^1];

lm=['s','o'];

gamma=[0.3 0.7];
%gamma=[0.5 0.5];
%gamma=[0.7 0.3];


colors=['k','k','k'];

%Size of the layers
K=[32 64];

number_of_layers=2;
number_of_packets = 200;
result = zeros(number_of_layers,number_of_packets);

hold on
for a=1:length(q)
    number_of_layers=2;
    result = zeros(number_of_layers,number_of_packets);
    for layer=1:number_of_layers
        
        S = zeros(K(layer)+1);
        S(1,1)=1;
        P = zeros(K(layer)+1);
        r=0:length(P);
        
        
        
        
        
        
        for packets=1:number_of_packets
            
            state=1;
            for n = 1:length(P)
                
                if n==length(P)
                    P(state,state)=1;
                else
                    P(state,state) = 1-gamma(layer)*(1-1/q(a)^(K(layer)-r(n)));
                    P(state,state+1) = gamma(layer)*(1-1/q(a)^(K(layer)-r(n)));
                end
                
                state=state+1;
            end
            
            
            
            new = S(1,:)*(P^packets);
            result(layer,packets) = result(layer,packets) + new(1,length(P));
            
        end
        
        
        if layer==1
            p=plot(1:2:number_of_packets,result(layer,1:2:number_of_packets))
            set(p,'Color',colors(layer),'Marker',lm(a),'MarkerSize',4)
        else
            result_to_be_plottet = result(layer,:);
            
            for n=1:(layer-1)
                result_to_be_plottet = result_to_be_plottet.*result(n,:);
            end
            p=plot(1:2:number_of_packets,result_to_be_plottet(1:2:number_of_packets));
            set(p,'Color',colors(layer),'Marker',lm(a),'MarkerSize',4,'LineStyle','--')
        end
        
    end
    
end

save('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep/difference_analysis/anal_g30','result')


axis([0 number_of_packets 0 1])

legend_h = legend('','','','');

% Legend fix
label_1=strcat('L1, \Gamma_1=',num2str(gamma(1)),', FF(2^1)',NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN);
label_2=strcat('L2 \cap L1, \Gamma_2=',num2str(gamma(2)),', FF(2^1)');
label_3=strcat('L1, \Gamma_1=',num2str(gamma(1)),', FF(2^8)',NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN);
label_4=strcat('L2 \cap L1, \Gamma_2=',num2str(gamma(2)),', FF(2^8)');

resize_legend(legend_h,0.7);
set(legend_h,'String',[label_1;label_2;label_3;label_4],'location','SouthEast')
set(legend_h,'interpreter','tex')
xlabel('Total number of received packets [-]')
ylabel('Decoding probability [-]')
set(gca,'XTick',0:25:number_of_packets)
set(gca,'YTick',0:0.1:1)




grid()
pbaspect([2.5 1 1])
% Save plot
figname=strcat('uep_new_analytic','_g1_',num2str(gamma(1)*100),'_g2_',num2str(gamma(2)*100),'.eps');
print(gcf,'-depsc2',figname)


hold off

